
package com.epfresh.supplier.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.activity.H5OrderDetailActivity;
import com.epfresh.supplier.entity.CategoryNames;
import com.epfresh.supplier.entity.OrderStoreDyn;
import com.epfresh.supplier.entity.OrderStoreDynStyle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class OrderStoreViewBinder
    extends ItemViewBinder<OrderStoreDyn, OrderStoreViewBinder.ViewHolder> implements View.OnClickListener{


   /* public static String STORE_DYN_TYPE_ORDER = "ORDER";//订单

    public static String STORE_DYN_TYPE_JMT = "JMT";//减免退

    public static String STORE_DYN_TYPE_COMMENT = "COMMENT";//评价

    public static String STORE_DYN_TYPE_PLATFORM = "PLATFORM";//台账

    public static String STORE_DYN_STYLE_ONE = "1";//样式一--大字

    public static String STORE_DYN_STYLE_TWO = "2";//样式一--小字*/

    public Map<String, Integer> mapRes = new HashMap<>();

    public OrderStoreViewBinder() {
        mapRes.clear();
        mapRes.put("ORDER", R.mipmap.icon_home_list_order);
        mapRes.put("JMT", R.mipmap.icon_home_list_halve);
        mapRes.put("COMMENT", R.mipmap.icon_home_list_comment);
        mapRes.put("PLATFORM", R.mipmap.icon_home_list_stand_book);
    }

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_store_dyn, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull OrderStoreDyn post) {
        holder.setData(post);
        holder.tvTitle.setText(post.getTitle());
        holder.tvTime.setText(post.getCreateTime());

        String type = post.getType();
        Integer res = null;
        if (type != null) {
            res = mapRes.get(type);
        }


        if (res != null) {
            holder.cover.setImageResource(res);
        } else {
            holder.cover.setImageDrawable(null);
        }

        List<OrderStoreDynStyle> textList = post.getTextList();

        if (textList != null) {
            StringBuilder sb = new StringBuilder();
            int k = 0;
            List<OrderStoreDynStyle> list = new ArrayList<>();
            for (int i = 0; i < textList.size(); i++) {
                OrderStoreDynStyle osd = textList.get(i);
                if (osd != null) {
                    String text = osd.getText();

                    if (text != null && text.length() > 0) {
                        if (k > 0) {
                            sb.append("\n\n");
                        }
                        list.add(osd);
                        k++;
                        sb.append(text);
                    }
                }
            }
            k = 0;
            int sl = 0;
            if (sb.length() > 0) {
                textList = list;
                SpannableStringBuilder sp = new SpannableStringBuilder(sb.toString());

                Log.e("textList111","textList:"+textList);
                Log.e("textList111","sb:"+sb);
                for (int i = 0; i < textList.size(); i++) {
                    OrderStoreDynStyle osd = textList.get(i);
                    if (osd != null) {
                        String text = osd.getText();

                        if (text != null && text.length() > 0) {

                            String style = osd.getStyle();
                            int s = 0;
                            int e = 0;
                            if (k >0){
                                sl += 2;
                                OrderStoreDynStyle osd1 = textList.get(i - 1);
                                String style1 = osd1.getStyle();
                                //相同5
                                if (style != null && style.equals(style1)) {
                                    sp.setSpan(new AbsoluteSizeSpan(0, true), sl - 1, sl, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体大小
                                }else {
                                    sp.setSpan(new AbsoluteSizeSpan(8, true), sl - 1, sl, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体大小
                                }
                            }
                            s = sl;
                            sl +=text.length();
                            e = sl;

                            if ("1".equals(style)) {

                                sp.setSpan(new ForegroundColorSpan(0xFF434343), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体颜色
                                sp.setSpan(new AbsoluteSizeSpan(14, true), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体大小

                            } else if ("2".equals(style)) {
                                sp.setSpan(new ForegroundColorSpan(0xFF898989), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体颜色
                                sp.setSpan(new AbsoluteSizeSpan(10, true), s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体大小
                            }
                            k++;

                        }
                    }
                }
                holder.tvContent.setText(sp);
            }else {

                holder.tvContent.setText("");
            }

        } else {
            holder.tvContent.setText("");
        }

        /*
        *   String name = "222";
            String uname = "111";
            String str = name + uname;
            SpannableStringBuilder sp = new  SpannableStringBuilder(str);
            sp.setSpan(new ForegroundColorSpan(0xFFFF0000), name.length(), str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体颜色
            sp.setSpan(new AbsoluteSizeSpan(10, true), name.length(), str.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体大小

        * */



        holder.itemView.setTag(R.id.item_key_position,getPosition(holder));
        holder.itemView.setTag(R.id.item_key_status,post);
        holder.itemView.setOnClickListener(this);

//        ImageLoader.getInstance().displayImage(post.getIcon(),holder.cover);
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);


            if (onRecyclerViewItemClickListener != null && o instanceof OrderStoreDyn){
                onRecyclerViewItemClickListener.onItemClick(v,position,o);
            }
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView cover;
        private TextView tvTitle;
        private TextView tvTime;
        private TextView tvContent;


        ViewHolder(@NonNull View itemView) {
            super(itemView);
            cover = (ImageView) itemView.findViewById(R.id.iv_icon);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);

        }


        void setData(@NonNull final OrderStoreDyn post) {

        }
    }
}
